﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReviewCurveApp.Classes
{
    public class Unit
    {
        private List<Task> _Tasks = new List<Task>();

        public int Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public string Context { get; set; }

        public UnitStatus Status { get; set; }

        public string SubjectInfo { get; set; }

        //public DateTime? BeginningTime 
        //{
        //    get 
        //    {
        //        return _ReviewRecords[0].ScheduledTime;
        //    }
        //    set 
        //    {
        //        ReAssign(value); 
        //    } 
        //}

        public List<Task> Tasks
        {
            get 
            {
                return _Tasks;
            }
        }

        public void ReAssign(DateTime? BeginSpot)
        {
            _Tasks.Clear();

            if (!BeginSpot.HasValue) return;

            DateTime startTime = BeginSpot.Value;
            IMemoryCurve Curve = CurveFactory.GetCurve();
            List<DateTime> times = Curve.GetRecitingSequnce(startTime);

            for (int i = 0; i < times.Count; i++)
            {
                Task newTask = new Task(i + 1, times[i], this.ToString());
                newTask.UnitId = Id;
                _Tasks.Add(newTask);
            }          

        }





        internal void clearTasks()
        {
            //ToDO
            throw new NotImplementedException();
        }

        internal IEnumerable<Task> GetTasksByDay(DateTime dateTime)
        {
            List<Task> tasks = new List<Task>();

            foreach (Task task in _Tasks)
            {
                if (task.ScheduledTime.Value.Date==dateTime.Date)
                {
                    tasks.Add(task);
                }
            }
            return tasks;
        }

        public DateTime CreateDateTime { get; set; }

        public DateTime ScheduledDateTime { get; set; }

        public int SubjectId { get; set; }

        internal string GetTasksInString()
        {
            string TasksString = "";

            foreach (Task task in _Tasks)
            {
                TasksString += "\t" + task.ToString() + "\r\n";

            }
            return TasksString;
        }

        public override string ToString()
        {
            return SubjectInfo+""+Name+" ";
        }

        public int Sequence { get; set; }
    }
}
